<template>
  <div class="wmc-form">
    <div class="header">
      <slot name="header"></slot>
    </div>
    <el-form
      :label-width="labelWidth"
      :label-position="labelPosition"
      ref="formRef"
    >
      <el-row>
        <template v-for="item in formItems" :key="item.label">
          <el-col v-bind="colLayout">
            <el-form-item
              v-if="!item.isHidden"
              :label="item.label"
              :rules="item.rules"
              :style="itemStyle"
            >
              <template
                v-if="item.type === 'input' || item.type === 'password'"
              >
                <el-input
                  :placeholder="item.placeholder"
                  v-bind="item.otherOptions"
                  :show-password="item.type === 'password'"
                  :model-value="modelValue[`${item.field}`]"
                  @update:modelValue="
                    handleValueChange($event, item.field ?? '')
                  "
                />
              </template>
              <template v-else-if="item.type === 'number'">
                <template v-if="item.field">
                  <el-input-number
                    :placeholder="item.placeholder"
                    v-bind="item.otherOptions"
                    :model-value="modelValue[`${item.field}`]"
                    @update:modelValue="
                      handleValueChange($event, item.field ?? '')
                    "
                  />
                </template>

                <template v-else>
                  <template v-for="i in item.fields" :key="i.field">
                    <el-input-number
                      :placeholder="item.placeholder"
                      v-bind="item.otherOptions"
                      :model-value="modelValue[`${i.field}`]"
                      @update:modelValue="handleValueChange($event, i.field)"
                    />
                  </template>
                </template>
              </template>
              <template v-else-if="item.type === 'select'">
                <el-select
                  :placeholder="item.placeholder"
                  v-bind="item.otherOptions"
                  style="width: 100%"
                  :model-value="modelValue[`${item.field}`]"
                  @update:modelValue="
                    handleValueChange($event, item.field ?? '')
                  "
                >
                  <el-option
                    v-for="option in item.options"
                    :key="option.value"
                    :value="option.value"
                    >{{ option.label }}</el-option
                  >
                </el-select>
              </template>
              <template v-else-if="item.type === 'datepicker'">
                <el-date-picker
                  style="width: 100%"
                  v-bind="item.otherOptions"
                  :model-value="modelValue[`${item.field}`]"
                  @update:modelValue="
                    handleValueChange($event, item.field ?? '')
                  "
                ></el-date-picker>
              </template>
              <template v-else-if="item.type === 'textarea'">
                <el-input
                  type="textarea"
                  :autosize="{ minRows: 5, maxRows: 10 }"
                  v-bind="item.otherOptions"
                  :model-value="modelValue[`${item.field}`]"
                  @update:model-value="
                    handleValueChange($event, item.field ?? '')
                  "
                ></el-input>
              </template>
              <template v-else-if="item.type === 'select-input'">
                <el-col :span="15">
                  <el-select
                    v-bind="item.otherOptions"
                    style="width: 100%"
                    :model-value="modelValue[`${item.fields[0].field}`]"
                    @update:modelValue="
                      handleValueChange($event, item.fields[0].field ?? '')
                    "
                  >
                    <el-option
                      v-for="option in item.options"
                      :key="option.value"
                      :value="option.value"
                    >
                      {{ option.label }}
                    </el-option>
                  </el-select>
                </el-col>
                <el-col :span="1"> - </el-col>
                <el-col :span="8">
                  <el-input
                    :placeholder="item.placeholder"
                    v-bind="item.otherOptions"
                    :model-value="modelValue[`${item.fields[1].field}`]"
                    @update:modelValue="
                      handleValueChange($event, item.fields[1].field ?? '')
                    "
                  />
                </el-col>
              </template>
              <template v-else-if="item.type === 'select-input-button'">
                <el-col :span="8">
                  <el-select
                    v-bind="item.otherOptions"
                    style="width: 100%"
                    :model-value="modelValue[`${item.fields[0].field}`]"
                    @update:modelValue="
                      handleValueChange($event, item.fields[0].field ?? '')
                    "
                  >
                    <el-option
                      v-for="option in item.options"
                      :key="option.value"
                      :value="option.value"
                    >
                      {{ option.label }}
                    </el-option>
                  </el-select>
                </el-col>
                <el-col :span="8">
                  <el-input
                    :placeholder="item.placeholder"
                    v-bind="item.otherOptions"
                    :model-value="modelValue[`${item.fields[1].field}`]"
                    @update:modelValue="
                      handleValueChange($event, item.fields[1].field ?? '')
                    "
                  />
                </el-col>
                <el-col :span="8">
                  <el-button type="primary">删除</el-button>
                </el-col>
              </template>
              <template v-else-if="item.type === 'input-input-button'">
                <el-col :span="8">
                  <el-input
                    :placeholder="item.placeholder"
                    v-bind="item.otherOptions"
                    :model-value="modelValue[`${item.fields[0].field}`]"
                    @update:modelValue="
                      handleValueChange($event, item.fields[0].field ?? '')
                    "
                  />
                </el-col>
                <el-col :span="8">
                  <el-input
                    :placeholder="item.placeholder"
                    v-bind="item.otherOptions"
                    :model-value="modelValue[`${item.fields[1].field}`]"
                    @update:modelValue="
                      handleValueChange($event, item.fields[1].field ?? '')
                    "
                  />
                </el-col>
                <el-col :span="8">
                  <el-button type="primary">删除</el-button>
                </el-col>
              </template>
            </el-form-item>
          </el-col>
        </template>
        <slot name="append-item"></slot>
      </el-row>
    </el-form>
    <div class="footer">
      <slot name="footer"></slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType } from 'vue'
import { IFormItem } from '../types'

export default defineComponent({
  props: {
    modelValue: {
      type: Object,
      required: true
    },
    formItems: {
      type: Array as PropType<IFormItem[]>,
      default: () => []
    },
    labelWidth: {
      type: String,
      default: '100px'
    },
    labelPosition: {
      type: String,
      default: 'left'
    },
    itemStyle: {
      type: Object,
      default: () => ({ padding: '10px 40px' })
    },
    colLayout: {
      type: Object,
      default: () => ({
        xl: 6, // >1920px 4个
        lg: 8,
        md: 12,
        sm: 24,
        xs: 24
      })
    },
    formRef: { type: Object }
  },
  emits: ['update:modelValue'],
  setup(props, { emit }) {
    const handleValueChange = (value: any, field: string) => {
      emit('update:modelValue', { ...props.modelValue, [field]: value })
    }

    return {
      handleValueChange
    }
  }
})
</script>

<style scoped lang="less">
.wmc-form {
  margin-bottom: -16px;
}
.el-input-number {
  margin-right: 20px;
}
</style>
